﻿<template>
  <a-modal
    title="编辑Project"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="Project Name" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="Please Input Project Name" v-decorator="['projectName']" />
        </a-form-item>
        <a-form-item label="Project Category" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="Please Input Project Category" v-decorator="['projectCategory']" />
        </a-form-item>
        <a-form-item label="Project Description" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-textarea placeholder="Please Input Project Description" v-decorator="['projectDescription']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
        <a-form-item label="Start Date" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="Please Select Start Date" v-decorator="['startDate']" @change="onChangestartDate"/>
        </a-form-item>
        <a-form-item label="End Date" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="Please Select End Date" v-decorator="['endDate']" @change="onChangeendDate"/>
        </a-form-item>
        <a-form-item label="Region" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="Please Input Region" v-decorator="['region']" />
        </a-form-item>
        <a-form-item label="Currency" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="Please Select Currency" v-decorator="['currency', {rules: [{ required: true, message: 'Please Select Currency！' }]}]">
            <a-select-option v-for="(item,index) in currencyData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="Budget" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="Please Input Budget" style="width: 100%" v-decorator="['budget']" />
        </a-form-item>
        <a-form-item label="Status" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="Please Select Status" v-decorator="['status', {rules: [{ required: true, message: 'Please Select Status！' }]}]">
            <a-select-option v-for="(item,index) in statusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    ProjectEdit
  } from '@/api/modular/main/ProjectManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        startDateDateString: '',
        endDateDateString: '',
        currencyData: [],
        statusData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        this.record = record
        const currencyOption = this.$options
        this.currencyData = currencyOption.filters['dictData']('currency')
        const statusOption = this.$options
        this.statusData = statusOption.filters['dictData']('project_status')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              projectName: record.projectName,
              projectCategory: record.projectCategory,
              projectDescription: record.projectDescription,
              region: record.region,
              currency: record.currency,
              budget: record.budget,
              status: record.status
            }
          )
        }, 100)
        this.form.getFieldDecorator('startDate', { initialValue: moment(record.startDate, 'YYYY-MM-DD') })
        this.startDateDateString = moment(record.startDate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('endDate', { initialValue: moment(record.endDate, 'YYYY-MM-DD') })
        this.endDateDateString = moment(record.endDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            values.startDate = this.startDateDateString
            this.record.startDate = this.startDateDateString
            values.endDate = this.endDateDateString
            this.record.endDate = this.endDateDateString
            ProjectEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangestartDate(date, dateString) {
        this.startDateDateString = dateString
      },
      onChangeendDate(date, dateString) {
        this.endDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
